#coding=utf8
# !coding:utf-8
# !/usr/bin/python
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import httplib
import json
import itchat, time
from itchat.content import *

@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
    message=msg.text
    if message[0]=="看":
        url = "http://huanxigeyingyuan.movie.lily789.com/extra/movmsg_get.php?name="+message[1:]
        print url
        conn = httplib.HTTPConnection("huanxigeyingyuan.movie.lily789.com")
        conn.request(method="GET", url=url)
        response = conn.getresponse()
        res = response.read()
        if res=='':
            retext="没有搜到相关视频，请您输入正确的关键字！"
        else:
            json_to_python = json.loads(res)
            if len(json_to_python)==1:
                retext = '帮您查询到 :'+"\r\n" + json_to_python[0]['d_name'] + "  " + json_to_python[0]['d_id']
            if len(json_to_python)>1:
                retext = '帮您查询到 :' + json_to_python[0]['d_name'] + "  " + json_to_python[0]['d_id']+"\r\n"+json_to_python[1]['d_name'] + "  " + json_to_python[1]['d_id']
        msg.user.send((retext))

@itchat.msg_register(TEXT, isGroupChat=True)
def text_reply(msg):
    print msg
    message = msg.text
    if message[0] == "看":
        url = "http://huanxigeyingyuan.movie.lily789.com/extra/movmsg_get.php?name=" + message[1:]
        print url
        conn = httplib.HTTPConnection("huanxigeyingyuan.movie.lily789.com")
        conn.request(method="GET", url=url)
        response = conn.getresponse()
        res = response.read()
        if res == '':
            retext = "没有搜到相关视频，请您输入正确的关键字！"
        else:
            json_to_python = json.loads(res)
            if len(json_to_python) == 1:
                retext = '帮您查询到 :' + "\r\n" + json_to_python[0]['d_name'] + "  " + json_to_python[0]['d_id']
            if len(json_to_python) > 1:
                retext = '帮您查询到 :' + json_to_python[0]['d_name'] + "  " + json_to_python[0]['d_id'] + "\r\n" + \
                         json_to_python[1]['d_name'] + "  " + json_to_python[1]['d_id']
        msg.user.send((retext))

@itchat.msg_register('Note', isGroupChat=True)
def simple_reply(msg):
    msg.user.send(("欢迎入群"))

itchat.auto_login()
itchat.run()